package com.epoint.mobileoa.ui.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.NewTask;
import com.epoint.mobileoa.bizlogic.SuperTabView;
import com.epoint.mobileoa.tools.XMLUtil;

/**
 * @author liyc
 * @time 2013-1-5 上午10:05:12
 * @annotation
 */
public class StatisticsMain extends SuperTabView {
	private ListView lvUse,lvLog;
	List<StatisticsModel> smList;
	List<EpointErrorLogModel> logList = new ArrayList<EpointErrorLogModel>();
	LogAdapter logAdapter = new LogAdapter();
	@Override
	public void initControls()
	{
		super.initControls();
		setShowViewTitleByTabButton(true);
		View llUse = inflate(R.layout.statisticsview, null);
		View lllog = inflate(R.layout.errorloglist, null);
		addTabInfo(
				new String[]{"使用统计","日志查询"}, 
				new View[]{llUse,lllog}
				);
		lvUse = (ListView) llUse.findViewById(R.id.lvTj);
		lvUse.setOnItemClickListener(this);
		lvLog = (ListView) lllog.findViewById(R.id.lv_log);
		addLogFoot();
		lvLog.setAdapter(logAdapter);
		lvLog.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		if(parent==lvLog)
		{
			if(position==logList.size()){
				getData_Log();
				return;
			}
			Intent intent = new Intent(this, ErrorLogDetail.class);
			intent.putExtra("model", logList.get(position));
			intent.putExtra("viewtitle", logList.get(position).manufacturer);
			startActivity(intent);
		}
	}

	@Override
	public int initTask(int index) {
		switch (index) {
		case 0:
			new GetStatisticsInfoNewTask(this, 1, null, null, 2);
			break;
		case 1:
			getData_Log();
			break;
		default:
			break;
		}
		return super.initTask(index);
	}

	private void getData_Log(){
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("CurrentPageIndex", CurrentPageIndex_log);
		param.put("PageSize", PageSize_log);
		new GetErrorLogNewTask(this,2,param, null, NewTask.ProgressTop);
		CurrentPageIndex_log ++;
	}

	private int CurrentPageIndex_log = 1;//第几页
	private int PageSize_log = 20;//每页显示数量
	private LinearLayout foot;
	public void addLogFoot(){
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		foot.setGravity(Gravity.CENTER);
		lvLog.addFooterView(foot);
		registerForContextMenu(lvLog);
	}

	@Override
	public void refreshForNewTask(int TaskId, Object... param) {
		if(TaskId==1)
		{
			String bs = param[0].toString();
			smList = XMLUtil.DomAnalysisCommon(bs, StatisticsModel.class);
			StatisticsAdapter adapter = new StatisticsAdapter();
			lvUse.setAdapter(adapter);
		}
		else if(TaskId==2)
		{
			String bs = param[0].toString();
			List<EpointErrorLogModel> logTempList = XMLUtil.DomAnalysisCommon(bs, EpointErrorLogModel.class);
			for(EpointErrorLogModel e:logTempList)
			{
				logList.add(e);
			}
			logAdapter.notifyDataSetChanged();
		}
	}

	class StatisticsAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return smList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(getContext());

				convertView = mInflater.inflate(R.layout.chuchaidetaillistview, null);

				holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
				holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
				holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
				holder.tv4 = (TextView) convertView.findViewById(R.id.tv4);
				holder.tv5 = (TextView) convertView.findViewById(R.id.tv5);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if(position>0){
				if(!smList.get(position-1).OUNAME.equals(smList.get(position).OUNAME)){
					holder.tv1.setBackgroundColor(getResources().getColor(R.color.black));
					holder.tv1.setVisibility(View.VISIBLE);
					holder.tv1.setText(Html.fromHtml("<font color="+getYGColor()+">部门:</font><font color=white>"+smList.get(position).OUNAME+"</font>"));
				}
				else{
					holder.tv1.setVisibility(View.GONE);
				}
			}
			else{
				holder.tv1.setVisibility(View.VISIBLE);
				holder.tv1.setBackgroundColor(getResources().getColor(R.color.black));
				holder.tv1.setText(Html.fromHtml("<font color="+getYGColor()+">部门:</font><font color=white>"+smList.get(position).OUNAME+"</font>"));
			}
			holder.tv2.setText(Html.fromHtml("<font color="+getYGColor()+">登陆人:</font><font color="+getBlackOrWhiteColor()+">"+smList.get(position).DISPLAYNAME+"</font>"));
			holder.tv3.setText(Html.fromHtml("<font color="+getYGColor()+">登陆次数:</font><font color="+getBlackOrWhiteColor()+">"+smList.get(position).LOGINTIMES+"</font>"));
			holder.tv4.setText(Html.fromHtml("<font color="+getYGColor()+">设备ID:</font><font color="+getBlackOrWhiteColor()+">"+smList.get(position).DEVICEID+"</font><br/>" +
					"<font color="+getYGColor()+">最后登录时间:</font><font color="+getBlackOrWhiteColor()+">"+smList.get(position).LASTLOGINDATE+"</font>"));
			holder.tv5.setVisibility(View.GONE);
			return convertView;
		}
	}

	public class ViewHolder {
		public TextView tv1;
		public TextView tv2;
		public TextView tv3;
		public TextView tv4;
		public TextView tv5;
	}

	class LogAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return logList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv = new TextView(getContext());
			EpointErrorLogModel e = logList.get(position);
			tv.setText(Html.fromHtml("<font color="+getYGColor()+"><big>"+"【"+(position+1)+"】"+e.displayname+"&nbsp;&nbsp;"+(e.releaseVersion==null?"":e.releaseVersion)+"</big></font><br/><font color="+getYGColor()+">"+e.DeviceId+"</font><br/><font color="+getYGColor()+">"+e.manufacturer+"</font><br/><font color="+getYGColor()+">"+e.SystemSDK+"</font><br/><font color="+getYGColor()+">"+e.OccurDate+"</font>"));
			return tv;
		}

	}

}
